/*
 * acooly.cn Inc.
 * Copyright (c) 2017 All Rights Reserved.
 * create by ouwen
 * date:2017-09-26
 */
package com.yiji.fintech.musfinance.service.impl;

import com.acooly.core.common.dao.support.PageInfo;
import com.acooly.core.common.service.EntityServiceImpl;
import com.yiji.fintech.musfinance.base.enums.NotifyStatusEnum;
import com.yiji.fintech.musfinance.base.enums.TransStatusEnum;
import com.yiji.fintech.musfinance.common.enums.ReviewStatusEnum;
import com.yiji.fintech.musfinance.common.exception.DBException;
import com.yiji.fintech.musfinance.dao.TransactionWithdrawDao;
import com.yiji.fintech.musfinance.entity.TransactionWithdraw;
import com.yiji.fintech.musfinance.service.TransactionWithdrawService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * center_transaction_withdraw Service实现
 *
 * Date: 2017-09-26 14:23:25
 *
 * @author ouwen
 *
 */
@Service("transactionWithdrawService")
public class TransactionWithdrawServiceImpl extends EntityServiceImpl<TransactionWithdraw, TransactionWithdrawDao> implements TransactionWithdrawService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<TransactionWithdraw> findAsynWithdrawSendToGateWay(Set<TransStatusEnum> statuses, Set<NotifyStatusEnum> notifyStatus , int pageSize, ReviewStatusEnum reviewStatus) {

        Map<String,Object> map = new HashMap<>();
        if(statuses!=null && statuses.size()>0) {
            map.put("IN_status", statuses);
        }
        if(notifyStatus!=null && notifyStatus.size()>0) {
            map.put("NOTIN_notifyStatus", notifyStatus);
        }
        if(reviewStatus!=null) {
            map.put("EQ_reviewStatus", reviewStatus);
        }
        PageInfo<TransactionWithdraw> pageInfo = new PageInfo<>();
        pageInfo.setCurrentPage(1);
        pageInfo.setCountOfCurrentPage(pageSize);
        try {
            pageInfo = getEntityDao().query(pageInfo,map,null);
        } catch (Exception e) {
            throw new DBException(e.getMessage());
        }
        return pageInfo.getPageResults();
    }

    @Override
    public int batchUpdateStatus(TransStatusEnum status, List<Long> ids) {

        StringBuffer sb = new StringBuffer();
        StringBuffer parames = new StringBuffer();
        sb.append("  UPDATE center_transaction_withdraw SET STATUS = '").append(status).append("' WHERE ID IN ( ");
        for(Long id :ids){
            parames.append(id).append(",");
        }
        String paramesStr = parames.toString();
        sb.append(paramesStr.substring(0,paramesStr.lastIndexOf(",")));
        sb.append(")");
        try {
            return jdbcTemplate.update(sb.toString());
        } catch (Exception e) {
            throw new DBException(e.getMessage());
        }
    }
    @Override
    public int updateStatus(TransStatusEnum status, String bizOrderNo) {
        StringBuffer sb = new StringBuffer("UPDATE center_transaction_withdraw  SET STATUS = ? WHERE BIZ_ORDER_NO = ?");
        try {
            return  jdbcTemplate.update(sb.toString(),status,bizOrderNo);
        } catch (Exception e) {
            throw new DBException(e.getMessage());
        }
    }
}
